package com.chenailin.www.util;

import com.chenailin.www.controller.*;
import com.chenailin.www.pojo.Administrator;
import com.chenailin.www.pojo.Student;
import com.chenailin.www.service.ReportService;

import java.util.Scanner;

public class MenuUtil {

    private static final Scanner sc = new Scanner(System.in);
    private static boolean returnToUpper = false;

    // 主菜单
    public static void showMainMenu() {
        while (!returnToUpper) {
            System.out.println("\n=== 校医院预约挂号系统 ===");
            System.out.println("1. 注册");
            System.out.println("2. 登录");
            System.out.println("0. 退出系统");
            System.out.print("请选择操作：");
            int choice = getValidChoice(0, 2);
            handleMainChoice(choice);
            if (returnToUpper) {
                returnToUpper = false;  // 重置状态
                return;
            }
        }
    }

    // 管理员菜单
    public static void showAdminMenu(Administrator administrator) {
        while (!returnToUpper) {
            System.out.println("\n=== 管理员功能菜单 ===");
            System.out.println("1. 医生管理");
            System.out.println("2. 科室管理");
            System.out.println("3. 排班管理");
            System.out.println("4. 修改密码");
            System.out.println("5. 查看统计报表");
            System.out.println("0. 退出登录");
            int choice = getValidChoice(0, 5);
            handleAdminChoice(choice, administrator);
            if (returnToUpper) {
                returnToUpper = false;  // 重置状态
                return;
            }
        }
    }

    // 学生菜单
    public static void showStudentMenu(Student stu) {
        while (!returnToUpper) {
            System.out.println("\n=== 患者功能菜单 ===");
            System.out.println("欢迎" + stu.getStuName());
            System.out.println("1. 预约挂号");
            System.out.println("2. 查看挂号记录");
            System.out.println("3. 取消预约");
            System.out.println("4. 修改个人信息");
            System.out.println("5. 修改密码");
            System.out.println("6. 身份验证");
            System.out.println("7. 评价系统");
            System.out.println("0. 退出登录");
            int choice = getValidChoice(0, 7);
            handleStudentChoice(choice, stu);
            if (returnToUpper) {
                returnToUpper = false;  // 重置状态
                return;
            }
        }
    }

    // 医生管理菜单
    public static void showDoctorMenu() {
        while (!returnToUpper) {
            System.out.println("\n=== 医生管理 ===");
            System.out.println("1. 添加医生");
            System.out.println("2. 修改医生");
            System.out.println("3. 删除医生");
            System.out.println("4. 查看所有医生");
            System.out.println("5. 查询医生个人信息");
            System.out.println("6. 查询医生评分");
            System.out.println("0. 返回上级");
            System.out.print("请选择操作：");
            int choice = getValidChoice(0, 6);
            handleDoctorChoice(choice);
            if (returnToUpper) {
                returnToUpper = false;  // 重置状态
                return;
            }
        }
    }

    // 时间管理菜单
    public static void showScheduleMenu() {
        while (!returnToUpper) {
            System.out.println("\n=== 排班管理 ===");
            System.out.println("1. 添加排班");
            System.out.println("2. 删除排班");
            System.out.println("3. 查看所有排班");
            System.out.println("0. 返回上级");
            System.out.print("请选择操作：");
            int choice = getValidChoice(0, 3);
            handleScheduleChoice(choice);
            if (returnToUpper) {
                returnToUpper = false;  // 重置状态
                return;
            }
        }
    }

    // 科室管理菜单
    public static void showDepartmentMenu() {
        while (!returnToUpper) {
            System.out.println("\n=== 科室管理 ===");
            System.out.println("1. 添加科室");
            System.out.println("2. 修改科室");
            System.out.println("3. 删除科室");
            System.out.println("4. 查看所有科室");
            System.out.println("5. 查看科室详情");
            System.out.println("6. 查询科室评分");
            System.out.println("0. 返回上级");
            System.out.print("请选择操作：");
            int choice = getValidChoice(0, 6);
            handleDepartmentChoice(choice);
            if (returnToUpper) {
                returnToUpper = false;  // 重置状态
                return;
            }
        }
    }

    // 主菜单实现逻辑
    private static void handleMainChoice(int choice) {
        try {
            PublicController publicController = new PublicController();
            switch (choice) {
                case 1:
                    publicController.register();
                    break;
                case 2:
                    publicController.login();
                    break;
                case 0:
                    if (getConfirmation("确定要退出登录吗？")) {
                        returnToUpper = true;
                        System.out.println("已安全退出系统");
                        return;
                    }
                    break;
                default:
                    System.out.println("无效选择");
            }
        } catch (Exception e) {
            System.err.println("操作失败: " + e.getMessage());
            System.out.println("按回车键继续...");
            sc.nextLine();
        }
    }

    // 管理员菜单实现逻辑
    private static void handleAdminChoice(int choice, Administrator administrator) {
        try {
            AdministratorController administratorController = new AdministratorController();
            ReportService reportService = new ReportService();
            switch (choice) {
                case 1:
                    MenuUtil.showDoctorMenu();
                    break;
                case 2:
                    MenuUtil.showDepartmentMenu();
                    break;
                case 3:
                    MenuUtil.showScheduleMenu();
                    break;
                case 4:
                    administratorController.modifyPassword(administrator);
                    break;
                case 5:
                    reportService.generateDailyReport();
                    break;
                case 0:
                    if (getConfirmation("确定要退出登录吗？")) {
                        returnToUpper = true;
                        System.out.println("已安全退出管理员账号");
                        return;
                    }
                    break;
                default:
                    System.out.println("无效选择");
            }
        } catch (Exception e) {
            System.err.println("操作失败: " + e.getMessage());
            System.out.println("按回车键继续...");
            sc.nextLine();
        }
    }

    // 学生菜单实现逻辑
    private static void handleStudentChoice(int choice, Student stu) {
        try {
            StudentController studentController = new StudentController();
            AppointmentController appointmentController = new AppointmentController();
            RatingController ratingController = new RatingController();
            switch (choice) {
                case 1:
                    appointmentController.appointmentRegister(stu);
                    break;
                case 2:
                    appointmentController.listAppointments(stu);
                    break;
                case 3:
                    appointmentController.cancelAppointment(stu);
                    break;
                case 4:
                    studentController.modifyStudent(stu);
                    break;
                case 5:
                    studentController.modifyPassword(stu);
                    break;
                case 6:
                    studentController.verifyStudent(stu);
                    break;
                case 7:
                    ratingController.addRating(stu);
                    break;
                case 0:
                    if (getConfirmation("确定要退出登录吗？")) {
                        returnToUpper = true;
                        System.out.println("已安全退出管理员账号");
                        return;
                    }
                    break;
                default:
                    System.out.println("无效选择");
            }
        } catch (Exception e) {
            System.err.println("操作失败: " + e.getMessage());
            System.out.println("按回车键继续...");
            sc.nextLine();
        }
    }

    // 医生菜单实现逻辑
    private static void handleDoctorChoice(int choice) {
        try {
            DoctorController doctorController = new DoctorController();
            RatingController ratingController = new RatingController();
            switch (choice) {
                case 1:
                    doctorController.addDoctor();
                    break;
                case 2:
                    doctorController.modifyDoctor();
                    break;
                case 3:
                    doctorController.deleteDoctor();
                    break;
                case 4:
                    doctorController.listDoctors();
                    break;
                case 5:
                    doctorController.showDoctorDetail();
                    break;
                case 6:
                    ratingController.showDoctorDetail();
                    break;
                case 0:
                    returnToUpper = true;
                    return;
                default:
                    System.out.println("无效选择");
            }
        } catch (Exception e) {
            System.err.println("操作失败: " + e.getMessage());
            System.out.println("按回车键继续...");
            sc.nextLine();
        }
    }

    // 时间菜单实现逻辑
    private static void handleScheduleChoice(int choice) {
        try {
            DoctorController doctorController = new DoctorController();
            switch (choice) {
                case 1:
                    doctorController.addSchedule();
                    break;
                case 2:
                    doctorController.deleteSchedule();
                    break;
                case 3:
                    doctorController.listDoctors();
                    break;
                case 0:
                    returnToUpper = true;
                    return;
                default:
                    System.out.println("无效选择");
            }
        } catch (Exception e) {
            System.err.println("操作失败: " + e.getMessage());
            System.out.println("按回车键继续...");
            sc.nextLine();
        }
    }

    // 科室菜单实现逻辑
    private static void handleDepartmentChoice(int choice) {
        try {
            DepartmentController departmentController = new DepartmentController();
            RatingController ratingController = new RatingController();
            switch (choice) {
                case 1:
                    departmentController.addDepartment();
                    break;
                case 2:
                    departmentController.modifyDepartment();
                    break;
                case 3:
                    departmentController.deleteDepartment();
                    break;
                case 4:
                    departmentController.listDepartments();
                    break;
                case 5:
                    departmentController.showDepartmentDetail();
                    break;
                case 6:
                    ratingController.showDepartmentDetail();
                    break;
                case 0:
                    returnToUpper = true;
                    return;
                default:
                    System.out.println("无效选择");
            }
        } catch (Exception e) {
            System.err.println("操作失败: " + e.getMessage());
            System.out.println("按回车键继续...");
            sc.nextLine();
        }
    }

    // 增强的输入验证方法
    public static int getValidChoice(int min, int max) {
        while (true) {
            System.out.print("请输入选择（" + min + "-" + max + "）：");
            try {
                String input = sc.nextLine().trim();
                int choice = Integer.parseInt(input);
                if (choice >= min && choice <= max) {
                    return choice;
                }
                System.out.println("输入超出范围，请重新输入");
            } catch (NumberFormatException e) {
                System.out.println("请输入有效的数字！");
            }
        }
    }

    // 确认提示
    public static boolean getConfirmation(String message) {
        while (true) {
            System.out.print(message + "\n(1.确认/0.取消): ");
            String input = sc.nextLine().trim().toUpperCase();
            switch (input) {
                case "1":
                    return true;
                case "0":
                    return false;
                default:
                    System.out.println("请输入 1 或 0！");
            }
        }
    }
}
